package __Goquery

import (
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"net/http"
	"strings"
)

func main() {
	//Document表示要爬取的文档
	url := "https://gorm.io/zh_CN/docs/"
	goquery.NewDocument(url)

	//响应结果
	res, _ := http.Get(url)
	dom, _ := goquery.NewDocumentFromResponse(res)
	dom.Find(".sidebar-link").Each(func(i int, s *goquery.Selection) {
		href, _ := s.Attr("href")
		text := s.Text()
		fmt.Println(i, href, text)
	})

	//响应内容
	doc, _ := goquery.NewDocumentFromReader(res.Body)
	doc.Find(".sidebar-link").Each(func(i int, s *goquery.Selection) {
		href, _ := s.Attr("href")
		text := s.Text()
		fmt.Println(i, href, text)
	})

	//或者
	html := `
			<body>
				<div>div1</div>
				<div>div2</div>
				<span>span</span>
			</body>
		`
	doz, _ := goquery.NewDocumentFromReader(strings.NewReader(html))
	doz.Find("div").Each(func(i int, s *goquery.Selection) {
		text := s.Text()
		fmt.Println(i, text)
	})

}
